/*
 * master_function_v4.c
 *
 * Code generation for function 'master_function_v4'
 *
 */

/* Include files */
#include "rt_nonfinite.h"
#include "master_function_v4.h"
#include "sum.h"
#include "master_function_v4_mexutil.h"
#include "master_function_v4_data.h"

/* Variable Definitions */
static emlrtRSInfo emlrtRSI = { 9, "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m"
};

static emlrtRSInfo b_emlrtRSI = { 10, "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m"
};

static emlrtRSInfo c_emlrtRSI = { 16, "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m"
};

static emlrtRSInfo d_emlrtRSI = { 17, "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m"
};

static emlrtRSInfo e_emlrtRSI = { 34, "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m"
};

static emlrtRSInfo f_emlrtRSI = { 40, "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m"
};

static emlrtRSInfo g_emlrtRSI = { 41, "find",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\elmat\\find.m"
};

static emlrtRSInfo h_emlrtRSI = { 68, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtRSInfo i_emlrtRSI = { 21, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtRSInfo j_emlrtRSI = { 54, "eml_xgemm",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\eml\\blas\\eml_xgemm.m"
};

static emlrtRSInfo k_emlrtRSI = { 1, "xgemm",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\coder\\coder\\+coder\\+internal\\+blas\\xgemm.p"
};

static emlrtMCInfo emlrtMCI = { 239, 9, "find",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\elmat\\find.m"
};

static emlrtMCInfo f_emlrtMCI = { 99, 13, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtMCInfo g_emlrtMCI = { 98, 23, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtMCInfo h_emlrtMCI = { 104, 13, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtMCInfo i_emlrtMCI = { 103, 23, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtBCInfo emlrtBCI = { -1, -1, 1, 1, "", "xgemm",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\coder\\coder\\+coder\\+internal\\+blas\\xgemm.p",
  0 };

static emlrtBCInfo b_emlrtBCI = { -1, -1, 24, 63, "x_0", "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo c_emlrtBCI = { -1, -1, 24, 37, "k_0", "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo d_emlrtBCI = { -1, -1, 37, 21, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo e_emlrtBCI = { -1, -1, 37, 24, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo f_emlrtBCI = { -1, -1, 37, 29, "diagonal",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo g_emlrtBCI = { -1, -1, 8, 35, "transitions_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo h_emlrtBCI = { -1, -1, 8, 38, "transitions_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo i_emlrtBCI = { -1, -1, 13, 27, "initial_rates",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo j_emlrtBCI = { -1, -1, 11, 27, "x", "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo k_emlrtBCI = { -1, -1, 20, 27, "initial_rates",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtDCInfo emlrtDCI = { 20, 27, "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  1 };

static emlrtBCInfo l_emlrtBCI = { -1, -1, 18, 27, "x", "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo m_emlrtBCI = { -1, -1, 24, 29, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo n_emlrtBCI = { -1, -1, 24, 32, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo o_emlrtBCI = { -1, -1, 26, 32, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo p_emlrtBCI = { -1, -1, 26, 35, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo q_emlrtBCI = { -1, -1, 27, 33, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtBCInfo r_emlrtBCI = { -1, -1, 27, 36, "rate_matrix",
  "master_function_v4",
  "G:\\Mingxi\\Dropbox\\Thesis Proposal\\codes\\trunk\\parameterfiting\\master_function_v4.m",
  0 };

static emlrtRSInfo n_emlrtRSI = { 239, "find",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\elmat\\find.m"
};

static emlrtRSInfo o_emlrtRSI = { 98, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtRSInfo p_emlrtRSI = { 103, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtRSInfo s_emlrtRSI = { 99, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

static emlrtRSInfo t_emlrtRSI = { 104, "eml_mtimes_helper",
  "C:\\Program Files\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\eml_mtimes_helper.m"
};

/* Function Definitions */
void master_function_v4(const emlrtStack *sp, real_T t, const real_T y_data[],
  const int32_T y_size[1], const real_T x_data[], const int32_T x_size[2], const
  real_T transitions_matrix_data[], const int32_T transitions_matrix_size[2],
  real_T force_rate, const real_T variables_indexes_data[], const int32_T
  variables_indexes_size[2], const real_T initial_rates_data[], const int32_T
  initial_rates_size[2], real_T dy_data[], int32_T dy_size[1])
{
  real_T s;
  real_T N;
  int32_T rate_matrix_size[2];
  int32_T ix;
  int32_T iy;
  real_T rate_matrix_data[100];
  int32_T n;
  int32_T i;
  int32_T b_n;
  int32_T j;
  boolean_T b_x_data[50];
  int32_T ixstart;
  int32_T ii_data[50];
  int32_T diagonal_size_idx_1;
  boolean_T exitg3;
  boolean_T guard2 = false;
  const mxArray *y;
  const mxArray *m0;
  int32_T b_ii_data[50];
  int32_T index_size[2];
  real_T index_data[50];
  int32_T k_0_size_idx_1;
  real_T k_0_data[50];
  real_T alpha1;
  boolean_T exitg2;
  boolean_T guard1 = false;
  const mxArray *b_y;
  real_T x_0_data[50];
  boolean_T p;
  boolean_T b_p;
  int32_T exitg1;
  const mxArray *c_y;
  static const int32_T iv0[2] = { 1, 30 };

  char_T cv0[30];
  static const char_T cv1[30] = { 'C', 'o', 'd', 'e', 'r', ':', 't', 'o', 'o',
    'l', 'b', 'o', 'x', ':', 's', 'u', 'm', '_', 's', 'p', 'e', 'c', 'i', 'a',
    'l', 'E', 'm', 'p', 't', 'y' };

  const mxArray *d_y;
  static const int32_T iv1[2] = { 1, 36 };

  char_T cv2[36];
  static const char_T cv3[36] = { 'C', 'o', 'd', 'e', 'r', ':', 't', 'o', 'o',
    'l', 'b', 'o', 'x', ':', 'a', 'u', 't', 'o', 'D', 'i', 'm', 'I', 'n', 'c',
    'o', 'm', 'p', 'a', 't', 'i', 'b', 'i', 'l', 'i', 't', 'y' };

  int8_T sz[2];
  real_T diagonal_data[10];
  const mxArray *e_y;
  static const int32_T iv2[2] = { 1, 45 };

  char_T cv4[45];
  static const char_T cv5[45] = { 'C', 'o', 'd', 'e', 'r', ':', 't', 'o', 'o',
    'l', 'b', 'o', 'x', ':', 'm', 't', 'i', 'm', 'e', 's', '_', 'n', 'o', 'D',
    'y', 'n', 'a', 'm', 'i', 'c', 'S', 'c', 'a', 'l', 'a', 'r', 'E', 'x', 'p',
    'a', 'n', 's', 'i', 'o', 'n' };

  const mxArray *f_y;
  static const int32_T iv3[2] = { 1, 21 };

  char_T cv6[21];
  static const char_T cv7[21] = { 'C', 'o', 'd', 'e', 'r', ':', 'M', 'A', 'T',
    'L', 'A', 'B', ':', 'i', 'n', 'n', 'e', 'r', 'd', 'i', 'm' };

  char_T TRANSB;
  char_T TRANSA;
  ptrdiff_t m_t;
  ptrdiff_t n_t;
  ptrdiff_t k_t;
  ptrdiff_t lda_t;
  ptrdiff_t ldb_t;
  ptrdiff_t ldc_t;
  double * alpha1_t;
  double * Aia0_t;
  double * Bib0_t;
  double * beta1_t;
  double * Cic0_t;
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack f_st;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &st;
  c_st.tls = st.tls;
  d_st.prev = &b_st;
  d_st.tls = b_st.tls;
  e_st.prev = &b_st;
  e_st.tls = b_st.tls;
  f_st.prev = &d_st;
  f_st.tls = d_st.tls;
  s = 1.0;
  N = (real_T)initial_rates_size[1] / 2.0;
  rate_matrix_size[0] = transitions_matrix_size[0];
  rate_matrix_size[1] = transitions_matrix_size[1];
  ix = transitions_matrix_size[0] * transitions_matrix_size[1];
  for (iy = 0; iy < ix; iy++) {
    rate_matrix_data[iy] = transitions_matrix_data[iy];
  }

  if ((0 == transitions_matrix_size[0]) || (0 == transitions_matrix_size[1])) {
    n = 0;
  } else {
    n = muIntScalarMax_sint32(transitions_matrix_size[0],
      transitions_matrix_size[1]);
  }

  i = 1;
  while (i - 1 <= n - 1) {
    if ((0 == transitions_matrix_size[0]) || (0 == transitions_matrix_size[1]))
    {
      b_n = 0;
    } else {
      b_n = muIntScalarMax_sint32(transitions_matrix_size[0],
        transitions_matrix_size[1]);
    }

    j = 1;
    while (j - 1 <= b_n - 1) {
      if (transitions_matrix_data[(emlrtDynamicBoundsCheckFastR2012b(i, 1,
            transitions_matrix_size[0], &g_emlrtBCI, sp) +
           transitions_matrix_size[0] * (emlrtDynamicBoundsCheckFastR2012b(j, 1,
             transitions_matrix_size[1], &h_emlrtBCI, sp) - 1)) - 1] > 0.0) {
        st.site = &emlrtRSI;
        ix = variables_indexes_size[0] * variables_indexes_size[1];
        for (iy = 0; iy < ix; iy++) {
          b_x_data[iy] = (variables_indexes_data[iy] == s);
        }

        b_st.site = &g_emlrtRSI;
        ixstart = 0;
        diagonal_size_idx_1 = variables_indexes_size[1];
        ix = 1;
        exitg3 = false;
        while ((!exitg3) && (ix <= variables_indexes_size[1])) {
          guard2 = false;
          if (b_x_data[ix - 1]) {
            ixstart++;
            ii_data[ixstart - 1] = ix;
            if (ixstart >= variables_indexes_size[1]) {
              exitg3 = true;
            } else {
              guard2 = true;
            }
          } else {
            guard2 = true;
          }

          if (guard2) {
            ix++;
          }
        }

        if (ixstart <= variables_indexes_size[1]) {
        } else {
          y = NULL;
          m0 = emlrtCreateString("Assertion failed.");
          emlrtAssign(&y, m0);
          d_st.site = &n_emlrtRSI;
          error(&d_st, y, &emlrtMCI);
        }

        if (variables_indexes_size[1] == 1) {
          if (ixstart == 0) {
            diagonal_size_idx_1 = 0;
          }
        } else {
          if (1 > ixstart) {
            diagonal_size_idx_1 = 0;
          } else {
            diagonal_size_idx_1 = ixstart;
          }

          for (iy = 0; iy < diagonal_size_idx_1; iy++) {
            b_ii_data[iy] = ii_data[iy];
          }

          for (iy = 0; iy < diagonal_size_idx_1; iy++) {
            ii_data[iy] = b_ii_data[iy];
          }
        }

        index_size[0] = 1;
        index_size[1] = diagonal_size_idx_1;
        ix = diagonal_size_idx_1;
        for (iy = 0; iy < ix; iy++) {
          index_data[iy] = ii_data[iy];
        }

        st.site = &b_emlrtRSI;
        if (sum(&st, index_data, index_size) > 0.0) {
          k_0_size_idx_1 = diagonal_size_idx_1;
          for (iy = 0; iy < diagonal_size_idx_1; iy++) {
            ixstart = (int32_T)index_data[iy];
            k_0_data[iy] = x_data[emlrtDynamicBoundsCheckFastR2012b(ixstart, 1,
              x_size[1], &j_emlrtBCI, sp) - 1];
          }
        } else {
          k_0_size_idx_1 = 1;
          iy = (int32_T)s;
          k_0_data[0] = initial_rates_data[emlrtDynamicBoundsCheckFastR2012b(iy,
            1, initial_rates_size[1], &i_emlrtBCI, sp) - 1];
        }

        st.site = &c_emlrtRSI;
        alpha1 = s + N;
        ix = variables_indexes_size[0] * variables_indexes_size[1];
        for (iy = 0; iy < ix; iy++) {
          b_x_data[iy] = (variables_indexes_data[iy] == alpha1);
        }

        b_st.site = &g_emlrtRSI;
        ixstart = 0;
        diagonal_size_idx_1 = variables_indexes_size[1];
        ix = 1;
        exitg2 = false;
        while ((!exitg2) && (ix <= variables_indexes_size[1])) {
          guard1 = false;
          if (b_x_data[ix - 1]) {
            ixstart++;
            ii_data[ixstart - 1] = ix;
            if (ixstart >= variables_indexes_size[1]) {
              exitg2 = true;
            } else {
              guard1 = true;
            }
          } else {
            guard1 = true;
          }

          if (guard1) {
            ix++;
          }
        }

        if (ixstart <= variables_indexes_size[1]) {
        } else {
          b_y = NULL;
          m0 = emlrtCreateString("Assertion failed.");
          emlrtAssign(&b_y, m0);
          d_st.site = &n_emlrtRSI;
          error(&d_st, b_y, &emlrtMCI);
        }

        if (variables_indexes_size[1] == 1) {
          if (ixstart == 0) {
            diagonal_size_idx_1 = 0;
          }
        } else {
          if (1 > ixstart) {
            diagonal_size_idx_1 = 0;
          } else {
            diagonal_size_idx_1 = ixstart;
          }

          for (iy = 0; iy < diagonal_size_idx_1; iy++) {
            b_ii_data[iy] = ii_data[iy];
          }

          for (iy = 0; iy < diagonal_size_idx_1; iy++) {
            ii_data[iy] = b_ii_data[iy];
          }
        }

        index_size[0] = 1;
        index_size[1] = diagonal_size_idx_1;
        ix = diagonal_size_idx_1;
        for (iy = 0; iy < ix; iy++) {
          index_data[iy] = ii_data[iy];
        }

        st.site = &d_emlrtRSI;
        if (sum(&st, index_data, index_size) > 0.0) {
          for (iy = 0; iy < diagonal_size_idx_1; iy++) {
            ixstart = (int32_T)index_data[iy];
            x_0_data[iy] = x_data[emlrtDynamicBoundsCheckFastR2012b(ixstart, 1,
              x_size[1], &l_emlrtBCI, sp) - 1];
          }
        } else {
          diagonal_size_idx_1 = 1;
          alpha1 = s + N;
          iy = (int32_T)emlrtIntegerCheckFastR2012b(alpha1, &emlrtDCI, sp);
          x_0_data[0] = initial_rates_data[emlrtDynamicBoundsCheckFastR2012b(iy,
            1, initial_rates_size[1], &k_emlrtBCI, sp) - 1];
        }

        /* x_0 */
        emlrtDynamicBoundsCheckFastR2012b(1, 1, k_0_size_idx_1, &c_emlrtBCI, sp);
        emlrtDynamicBoundsCheckFastR2012b(1, 1, diagonal_size_idx_1, &b_emlrtBCI,
          sp);
        rate_matrix_data[(emlrtDynamicBoundsCheckFastR2012b(i, 1,
          transitions_matrix_size[0], &m_emlrtBCI, sp) +
                          transitions_matrix_size[0] *
                          (emlrtDynamicBoundsCheckFastR2012b(j, 1,
          transitions_matrix_size[1], &n_emlrtBCI, sp) - 1)) - 1] = k_0_data[0] *
          muDoubleScalarExp(t * force_rate * x_0_data[0] / 4.1);
        if (rate_matrix_data[(emlrtDynamicBoundsCheckFastR2012b(i, 1,
              transitions_matrix_size[0], &o_emlrtBCI, sp) +
                              transitions_matrix_size[0] *
                              (emlrtDynamicBoundsCheckFastR2012b(j, 1,
               transitions_matrix_size[1], &p_emlrtBCI, sp) - 1)) - 1] >= 5000.0)
        {
          rate_matrix_data[(emlrtDynamicBoundsCheckFastR2012b(i, 1,
            transitions_matrix_size[0], &q_emlrtBCI, sp) +
                            transitions_matrix_size[0] *
                            (emlrtDynamicBoundsCheckFastR2012b(j, 1,
            transitions_matrix_size[1], &r_emlrtBCI, sp) - 1)) - 1] = 5000.0;
        }

        s++;
      }

      j++;
      emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
    }

    i++;
    emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
  }

  st.site = &e_emlrtRSI;
  p = false;
  b_p = false;
  ixstart = 0;
  do {
    exitg1 = 0;
    if (ixstart < 2) {
      if (rate_matrix_size[ixstart] != 0) {
        exitg1 = 1;
      } else {
        ixstart++;
      }
    } else {
      b_p = true;
      exitg1 = 1;
    }
  } while (exitg1 == 0);

  if (!b_p) {
  } else {
    p = true;
  }

  if (!p) {
  } else {
    c_y = NULL;
    m0 = emlrtCreateCharArray(2, iv0);
    for (i = 0; i < 30; i++) {
      cv0[i] = cv1[i];
    }

    emlrtInitCharArrayR2013a(&st, 30, m0, cv0);
    emlrtAssign(&c_y, m0);
    b_st.site = &m_emlrtRSI;
    c_st.site = &r_emlrtRSI;
    error(&b_st, message(&c_st, c_y, &b_emlrtMCI), &c_emlrtMCI);
  }

  if (((transitions_matrix_size[0] == 1) && (transitions_matrix_size[1] == 1)) ||
      (transitions_matrix_size[0] != 1)) {
    p = true;
  } else {
    p = false;
  }

  if (p) {
  } else {
    d_y = NULL;
    m0 = emlrtCreateCharArray(2, iv1);
    for (i = 0; i < 36; i++) {
      cv2[i] = cv3[i];
    }

    emlrtInitCharArrayR2013a(&st, 36, m0, cv2);
    emlrtAssign(&d_y, m0);
    b_st.site = &l_emlrtRSI;
    c_st.site = &q_emlrtRSI;
    error(&b_st, message(&c_st, d_y, &d_emlrtMCI), &e_emlrtMCI);
  }

  for (iy = 0; iy < 2; iy++) {
    sz[iy] = (int8_T)rate_matrix_size[iy];
  }

  diagonal_size_idx_1 = sz[1];
  if ((transitions_matrix_size[0] == 0) || (transitions_matrix_size[1] == 0)) {
    diagonal_size_idx_1 = sz[1];
    ix = sz[1];
    for (iy = 0; iy < ix; iy++) {
      diagonal_data[iy] = 0.0;
    }
  } else {
    ix = -1;
    iy = -1;
    for (i = 1; i <= transitions_matrix_size[1]; i++) {
      ixstart = ix + 1;
      ix++;
      s = rate_matrix_data[ixstart];
      for (ixstart = 2; ixstart <= transitions_matrix_size[0]; ixstart++) {
        ix++;
        s += rate_matrix_data[ix];
      }

      iy++;
      diagonal_data[iy] = s;
    }
  }

  for (iy = 0; iy < diagonal_size_idx_1; iy++) {
    diagonal_data[iy] = -diagonal_data[iy];
  }

  if ((0 == transitions_matrix_size[0]) || (0 == transitions_matrix_size[1])) {
    n = 0;
  } else {
    n = muIntScalarMax_sint32(transitions_matrix_size[0],
      transitions_matrix_size[1]);
  }

  i = 1;
  while (i - 1 <= n - 1) {
    rate_matrix_data[(emlrtDynamicBoundsCheckFastR2012b(i, 1,
      transitions_matrix_size[0], &d_emlrtBCI, sp) + transitions_matrix_size[0] *
                      (emlrtDynamicBoundsCheckFastR2012b(i, 1,
      transitions_matrix_size[1], &e_emlrtBCI, sp) - 1)) - 1] =
      diagonal_data[emlrtDynamicBoundsCheckFastR2012b(i, 1, diagonal_size_idx_1,
      &f_emlrtBCI, sp) - 1];
    i++;
    emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
  }

  st.site = &f_emlrtRSI;
  b_st.site = &i_emlrtRSI;
  if (!(transitions_matrix_size[1] == y_size[0])) {
    if (((transitions_matrix_size[0] == 1) && (transitions_matrix_size[1] == 1))
        || (y_size[0] == 1)) {
      e_y = NULL;
      m0 = emlrtCreateCharArray(2, iv2);
      for (i = 0; i < 45; i++) {
        cv4[i] = cv5[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 45, m0, cv4);
      emlrtAssign(&e_y, m0);
      d_st.site = &o_emlrtRSI;
      e_st.site = &s_emlrtRSI;
      error(&d_st, message(&e_st, e_y, &f_emlrtMCI), &g_emlrtMCI);
    } else {
      f_y = NULL;
      m0 = emlrtCreateCharArray(2, iv3);
      for (i = 0; i < 21; i++) {
        cv6[i] = cv7[i];
      }

      emlrtInitCharArrayR2013a(&b_st, 21, m0, cv6);
      emlrtAssign(&f_y, m0);
      d_st.site = &p_emlrtRSI;
      e_st.site = &t_emlrtRSI;
      error(&d_st, message(&e_st, f_y, &h_emlrtMCI), &i_emlrtMCI);
    }
  }

  if ((transitions_matrix_size[1] == 1) || (y_size[0] == 1)) {
    dy_size[0] = transitions_matrix_size[0];
    ix = transitions_matrix_size[0];
    for (iy = 0; iy < ix; iy++) {
      dy_data[iy] = 0.0;
      diagonal_size_idx_1 = transitions_matrix_size[1];
      for (ixstart = 0; ixstart < diagonal_size_idx_1; ixstart++) {
        dy_data[iy] += rate_matrix_data[iy + transitions_matrix_size[0] *
          ixstart] * y_data[ixstart];
      }
    }
  } else {
    b_st.site = &h_emlrtRSI;
    d_st.site = &j_emlrtRSI;
    dy_size[0] = (int8_T)transitions_matrix_size[0];
    ix = (int8_T)transitions_matrix_size[0];
    for (iy = 0; iy < ix; iy++) {
      dy_data[iy] = 0.0;
    }

    if ((transitions_matrix_size[0] < 1) || (transitions_matrix_size[1] < 1)) {
    } else {
      f_st.site = &k_emlrtRSI;
      alpha1 = 1.0;
      s = 0.0;
      TRANSB = 'N';
      TRANSA = 'N';
      m_t = (ptrdiff_t)(transitions_matrix_size[0]);
      n_t = (ptrdiff_t)(1);
      k_t = (ptrdiff_t)(transitions_matrix_size[1]);
      lda_t = (ptrdiff_t)(transitions_matrix_size[0]);
      ldb_t = (ptrdiff_t)(transitions_matrix_size[1]);
      ldc_t = (ptrdiff_t)(transitions_matrix_size[0]);
      alpha1_t = (double *)(&alpha1);
      Aia0_t = (double *)(&rate_matrix_data[0]);
      emlrtDynamicBoundsCheckFastR2012b(1, 1, y_size[0], &emlrtBCI, &f_st);
      Bib0_t = (double *)(&y_data[0]);
      beta1_t = (double *)(&s);
      iy = (int8_T)transitions_matrix_size[0];
      emlrtDynamicBoundsCheckFastR2012b(1, 1, iy, &emlrtBCI, &f_st);
      Cic0_t = (double *)(&dy_data[0]);
      dgemm(&TRANSA, &TRANSB, &m_t, &n_t, &k_t, alpha1_t, Aia0_t, &lda_t, Bib0_t,
            &ldb_t, beta1_t, Cic0_t, &ldc_t);
    }
  }
}

/* End of code generation (master_function_v4.c) */
